import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import com.qiniu.cdn.CdnManager;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Main {

    private static final String SSL_CERT_UPLOAD_API = "https://api.qiniu.com/sslcert";
    private static final String SSL_CERT_DEPLOY_API = "https://api.qiniu.com/domain/%s/httpsconf";
    private static  Auth auth;
    public static void main(String[] args) throws Exception {
        //加载arg的map
        Map<String, String> argsMap = new HashMap<>();
        for (int i = 0; i < args.length; i++) {
            String[] split = args[i].split("=");
            if (split.length != 2) {
                continue;
            }
            argsMap.put(split[0], split[1]);
        }
        System.out.println(argsMap);
        auth = Auth.create(argsMap.get("accessKey"), argsMap.get("secretKey"));
        String certId = uploadCert(argsMap.get("certPath"), argsMap.get("keyPath"));
        System.out.println("已传证书编号："+certId);
        deployCert(argsMap.get("domain"), certId);
    }
    public static String uploadCert(String certPath, String keyPath) throws IOException {
        JSONObject  m = new JSONObject();
        m.put("name", "test");
        m.put("common_name", "test");
        m.put("ca", String.join("\n", Files.readAllLines(Paths.get(certPath))));
        m.put("pri", String.join("\n", Files.readAllLines(Paths.get(keyPath))));

        String body = m.toString();
        byte[] bodyByte = body.getBytes();
        // 获取签名
        String accessToken =  auth.signRequest(SSL_CERT_UPLOAD_API, bodyByte, "application/json");

        //使用hutool实现
        HttpRequest request = HttpRequest.post(SSL_CERT_UPLOAD_API)
                .header("Authorization", "QBox "+accessToken)
                .body(body, "application/json");
        String bodyResult = request.execute().body();
        System.out.println(bodyResult);
        JSONObject json = JSONObject.parseObject(bodyResult);
        String certId = json.getString("certID");
        return certId;
    }
    public static void deployCert(String domain,String certId) throws IOException {
        JSONObject  m = new JSONObject();
        m.put("certId", certId);
        m.put("forceHttps", false);
        m.put("http2Enable", true);

        String body = m.toString();
        byte[] bodyByte = body.getBytes();
        // 获取签名
        String url = SSL_CERT_DEPLOY_API.replace("%s",domain);
        String accessToken =  auth.signRequest(url,   bodyByte, "application/json");

        //使用hutool实现
        HttpRequest request = HttpRequest.put(url)
                .header("Authorization", "QBox "+accessToken)
                .body(body, "application/json");
        String bodyResult = request.execute().body();
        System.out.println(bodyResult);
    }

}
